// Copyright 2011 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#pragma once

#include <atlbase.h>
#include <atlcom.h>
#include <atlctl.h>
#include <shlguid.h>
#include <exdispid.h>
#include <shlobj.h>
#include "kmoo.h"  // Generated by the MIDL compiler.
#include "resource.h"

// Once loaded, this BHO ensures that the NAI opt out cookies are persistently
// set, by resetting it every time a new page is loaded.
class ATL_NO_VTABLE KeepMyOptOutsBHO
    : public ATL::CComObjectRootEx<ATL::CComSingleThreadModel>,
      public ATL::CComCoClass<KeepMyOptOutsBHO, &CLSID_KeepMyOptOutsBHO>,
      public ATL::IObjectWithSiteImpl<KeepMyOptOutsBHO>,
      public ATL::IDispatchImpl<IKeepMyOptOutsBHO, &IID_IKeepMyOptOutsBHO,
                                &LIBID_KeepMyOptOutsLib, 1, 0>,
      public ATL::IDispEventImpl<1, KeepMyOptOutsBHO,
                                 &DIID_DWebBrowserEvents2,
                                 &LIBID_SHDocVw, 1, 1> {
 public:
  KeepMyOptOutsBHO() { }

  STDMETHOD(SetSite)(IUnknown *site);

  DECLARE_REGISTRY_RESOURCEID(IDR_KEEPMYOPTOUTSBHO)

  BEGIN_COM_MAP(KeepMyOptOutsBHO)
    COM_INTERFACE_ENTRY(IKeepMyOptOutsBHO)
    COM_INTERFACE_ENTRY(IDispatch)
    COM_INTERFACE_ENTRY(IObjectWithSite)
  END_COM_MAP()

  BEGIN_SINK_MAP(KeepMyOptOutsBHO)
    SINK_ENTRY_EX(1, DIID_DWebBrowserEvents2,
                  DISPID_BEFORENAVIGATE2, OnBeforeNavigate)
  END_SINK_MAP()

  void STDMETHODCALLTYPE OnBeforeNavigate(IDispatch *dispatch,
                                          VARIANT *url,
                                          VARIANT *flags,
                                          VARIANT *target_frame_name,
                                          VARIANT *post_data,
                                          VARIANT *headers,
                                          VARIANT_BOOL *cancel);
 private:
  ATL::CComPtr<IWebBrowser2> web_browser_;
  bool advised_;
};

OBJECT_ENTRY_AUTO(__uuidof(KeepMyOptOutsBHO), KeepMyOptOutsBHO)
